<template>
  <div>

    <el-dialog title="添加组织" :visible.sync="dialogFormVisible">
      <el-form :model="form" name="el-form-sys-role-edit-name" id="el-form-sys-role-edit-id"  size="mini">
        <el-form-item label="添加组织" :label-width="formLabelWidth">
          <el-input v-model="form.name" name="roleName" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="上级组织" :label-width="formLabelWidth">
          <el-input v-model="form.pname" name="pname" v-on:click.native="_pnameClick" :disabled="true" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="form.seq" name="seq" autocomplete="off"></el-input>
        </el-form-item>



        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.remark" name="remark" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="_ok">确 定</el-button>
      </div>
    </el-dialog>


    <SysDeptSelect ref="sysDeptSelectModel" @on-result-change="_selectBack"></SysDeptSelect>
  </div>

</template>

<script>

  import  SysDeptSelect from './SysDeptSelect'
    export default {
      components:{
        SysDeptSelect
      },
      mounted(){
        this._listParent();
      },

        name: "SysMenuEdit",
      data(){
          return{

            formAdd:{
              name:'',
              pname:'',
              seq:'',
              remark:'',
              pid:''
            },
            form:{
              name:'',
              pname:'',
              seq:'',
              remark:'',
              pid:''
            },

            pNameList:[],
            parentName: '',

            dialogFormVisible: false,

            formLabelWidth: '120px'
          }
      },
      methods:{

          _open(obj){

            if (undefined===obj){
              this.pname='';
              this.form= JSON.parse(JSON.stringify(this.formAdd));;
              console.log('-----formAdd------',this.formAdd)
              this.dialogFormVisible=true;
            }else{
              this.form=obj;
              this.pname=obj.pname;
              this.dialogFormVisible=true;
            }

          },

        _pNameChange(data){
            this.form.pid=data;
        },
        _selectBack(data){
           this.form.pname=data.label;
           this.form.pid = data.id;
        },

        _pnameClick(){

          this.$refs.sysDeptSelectModel._open();
        },


        _listParent(){
          this.$http.get('/sys_resource/list_parent').then(res=>{
                this.pNameList=res.data.data;
          }).catch(err=>{

          });
        },


          _ok(){
            let userData= this.form;
            if (undefined === userData.id){
              this.$http.post('/sys_dept/add',userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '添加失败'
                });
              })

            } else {
              this.$http.put('/sys_resource/'+userData.id,userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '编辑失败'
                });
              })
            }




          }

      },
    }
</script>

<style scoped>

</style>
